package br.edu.univas.locadoragraph.dao;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;

import br.edu.univas.locadoragraph.database.Connection;
import br.edu.univas.locadoragraph.database.Keys;
import br.edu.univas.locadoragraph.to.DirectorTO;

public class DirectorDAO {

	private Connection conn = null;

	public DirectorDAO() {
		conn = Connection.getInstance();
	}

	public boolean saveDirectorGraph(DirectorTO director) {
		System.out.println("Method: Save Director ");
		long start = System.currentTimeMillis();
		GraphDatabaseService graph = conn.newGraphDatabase();
		Index<Node> index = graph.index().forNodes(Keys.INDEXDIRECTOR);
		Node node = index.get(Keys.DIRECTORKEY,
				director.getName().toLowerCase()).getSingle();
		if (node == null) {
			Transaction tx = graph.beginTx();
			try {
				node = graph.createNode();
				node.setProperty(Keys.DIRECTORKEY, director.getName()
						.toLowerCase());
				node.setProperty("gender", director.getGender().toLowerCase());
				index.add(node, Keys.DIRECTORKEY, director.getName()
						.toLowerCase());
				tx.success();
				tx.finish();
				//graph.shutdown();
				double total = (System.currentTimeMillis() - start);
				System.out.println("Director saved! Time: " + total / 1000 );
				return true;
			} catch (Exception e) {
				tx.failure();
				tx.finish();
			} 
		}
		//graph.shutdown();
		double total = (System.currentTimeMillis() - start);
		System.out.println("Director not saved! Time: " + total / 1000);
		return false;
	}

	public List<DirectorTO> getAllDirector() {
		System.out.println("Method: Get all Directors ");
		Long start = System.currentTimeMillis();
		List<DirectorTO> directors = new ArrayList<DirectorTO>();
		GraphDatabaseService graph = conn.newGraphDatabase();
		Index<Node> index = graph.index().forNodes(Keys.INDEXDIRECTOR);
		Iterable<Node> nodes = index.query(Keys.DIRECTORKEY, "*");
		for (Node node : nodes) {
			DirectorTO directorTO = new DirectorTO();
			directorTO.setName((String) node.getProperty(Keys.DIRECTORKEY));
			directorTO.setGender((String) node.getProperty("gender"));
			directors.add(directorTO);
		}
		//graph.shutdown();
		double total = (System.currentTimeMillis() - start);
		System.out.println("Time: " + total / 1000);
		return directors;
	}

	public boolean updateDirectorGraph(DirectorTO director, String key) {

		GraphDatabaseService graph = conn.newGraphDatabase();
		Node node = getNodeDirector(graph, key);
		if (node != null) {
			Transaction tx = graph.beginTx();
			try {
				node.setProperty(Keys.DIRECTORKEY, director.getName().toLowerCase() );
				node.setProperty("gender", director.getGender().toLowerCase());
				
				tx.success();
				tx.finish();
				return true;
			} catch (Exception e) {
				tx.failure();
				tx.finish();
				System.err.println("Erro: " + e);
			}
			
	
		}
		return false;
		
	}

	private Node getNodeDirector(GraphDatabaseService graph, String key) {
		Index<Node> index = graph.index().forNodes(Keys.INDEXDIRECTOR);
		Node node = index.get(Keys.USERKEY, key.toLowerCase()).getSingle();
		return node;
	}

}
